package com.sc.nft.dao;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.SecondOrderSettle;
import com.sc.nft.enums.DivideTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.mapper.SecondOrderSettleMapper;
import com.sc.nft.sup.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * (sc_first_order_settle)数据DAO
 *
 * @author zxy
 * @description 商户banner
 * @since 2023-12-06 20:29:39
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class SecondOrderSettleDao {

    private final SecondOrderSettleDaoAdapter adapter;

    public boolean save(SecondOrderSettle entity) {
        return adapter.save(entity);
    }

    @Repository
    static class SecondOrderSettleDaoAdapter extends ServiceImpl<SecondOrderSettleMapper, SecondOrderSettle> {

    }

    public List<SecondOrderSettle> getWaitSettle() {
        // 每日由定时任务结算 is_settle = false 数据只会是近期数据，冷热分离保留2个月数据
        return adapter.lambdaQuery()
                .eq(SecondOrderSettle::getDivideType, DivideTypeEnum.DAO)
                .eq(SecondOrderSettle::getIsSettle, false)
                .last("limit 1000")
                .list();
    }

    public void updateSettle(Long id) {
        Assert.isTrue(adapter.lambdaUpdate()
                        .eq(SecondOrderSettle::getId, id)
                        .eq(SecondOrderSettle::getIsSettle, false)
                        .set(SecondOrderSettle::getIsSettle, true)
                        .update(),
                () -> new GlobalRunTimeException(ErrorCode.UPDATE_ERROR));
    }
}